package cn.lovecyy.mockjson.core.type;

import cn.hutool.core.util.StrUtil;
import cn.lovecyy.mockjson.core.domain.TypeConfig;

/**
 * TimestampTypeMocker
 *
 * @author Yakir
 */
public class TimestampTypeMocker implements TypeMocker {

	@Override
	public String getType() {
		return "timestamp";
	}

	@Override
	public Object mock(TypeConfig config) {
		String param = config.getParam();
		String mainParam = config.getMainParam();
		long timestamp;
		if (StrUtil.isEmpty(mainParam)) {
			timestamp = System.currentTimeMillis();
		}
		else {
			long intParam = Long.parseLong(mainParam);
			int length = StrUtil.length(mainParam);
			if (length == 10) {
				intParam = intParam * 1000;
			}
			timestamp = intParam;
		}
		String format = config.getFormat();
		long retVal;
		if (StrUtil.isEmpty(format)) {
			// 默认13位
			retVal = timestamp;
		}
		else {
			int fmtLength = Integer.parseInt(format);
			if (fmtLength == 10) {
				retVal = timestamp / 1000;
			}
			else {
				retVal = timestamp;
			}

		}
		return retVal;
	}

}
